import { findHeadCategory } from '@/api/cate'
import { category } from '@/utils/constant'

export default {
  namespaced: true,
  state () {
    return {
      list: category.map((item, i) => ({ id: i, name: item })) // 分类列表
    }
  },
  mutations: {
    // 设置分类列表
    updateCate (state, payload) {
      state.list = payload
    }
  },
  actions: {
    // 获取分类列表请求
    async findHeadCategory (context) {
      const list = await findHeadCategory()
      context.commit('updateCate', list.result)
    }
  },
  getters: {
    leftCate (state) {
      return state.list.map(item => {
        return {
          ...item,
          children: item.children && item.children.filter((tag, ind) => ind < 2)
        }
      })
    }
  }
}
